package com.xiaolanshu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaolanshu.entity.Goods;
import com.xiaolanshu.entity.GoodsComment;
import com.xiaolanshu.entity.Order;
import com.xiaolanshu.entity.User;
import com.xiaolanshu.mapper.GoodsCommentMapper;
import com.xiaolanshu.mapper.GoodsMapper;
import com.xiaolanshu.mapper.UserMapper;
import com.xiaolanshu.service.GoodsService;
import com.xiaolanshu.vo.GoodsInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsCommentMapper goodsCommentMapper;
    @Autowired
    private UserMapper userMapper;

    //获取商品列表
    @Override
    public List<Goods> goodsList() {
        //构建条件，查询的类型为Goods对象
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Goods::getDeleted,0);
        return  goodsMapper.selectList(lqw);
    }
    //根据商品id获取商品内容
    @Override
    public GoodsInfoVo findGoodsVoById(Long goodsId) {
        //查询id商品内容
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Goods::getId,goodsId);
        Goods goods = goodsMapper.selectOne(lqw);

        //根据商品id查询评论列表
        LambdaQueryWrapper<GoodsComment> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(GoodsComment::getGoodsId,goods.getId());
        List<GoodsComment> goodsComments = goodsCommentMapper.selectList(lqw2);

        //遍历评论列表,并添加用户名和用户头像
        List<GoodsComment> goodsCommentList = new ArrayList<>();

        for (GoodsComment g:goodsComments
             ) {
            //根据评论表的用户id获取用户信息
            LambdaQueryWrapper<User> lqw3 = new LambdaQueryWrapper<>();
            lqw3.select(User::getUsername,User::getHeadphoto,User::getCreated).eq(User::getId,g.getUserId());
            User user = userMapper.selectOne(lqw3);

            //将用户信息保存到评论中
            g.setUsername(user.getUsername());
            g.setHeadphoto(user.getHeadphoto());
            g.setCreated(user.getCreated());

            goodsCommentList.add(g);

        }
        //创建商品内容业务对象保存商品内容和评论信息
        GoodsInfoVo goodsInfoVo = new GoodsInfoVo();
        goodsInfoVo.setGoods(goods);
        goodsInfoVo.setGoodsComment(goodsCommentList);

        return goodsInfoVo;

    }
    //通过userId和goodsId查询商品信息
    @Override
    public Goods findGoods( Long goodsId) {
        //构造查询条件
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Goods::getId,goodsId);

        return goodsMapper.selectOne(lqw);
    }
    //根据商品名，标签，商品介绍模糊查询
    @Override
    public List<Goods> search(String param) {

        //构造查询条件
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.like(Goods::getGoodsName,param).or()
                .like(Goods::getGoodsTag,param).or()
                .like(Goods::getGoodsContent,param);
        return goodsMapper.selectList(lqw);
    }
    //对商品进行评论
    @Override
    public int goodsComment(String comment, Long goodsId, Long userId) {
        //保存信息
        GoodsComment goodsComment = new GoodsComment();
        goodsComment.setComment(comment);
        goodsComment.setUserId(userId);
        goodsComment.setGoodsId(goodsId);

        return goodsCommentMapper.insert(goodsComment);

    }
    //推荐商品列表
    @Override
    public List<Goods> recommendGoodsList(int[] array) {

        List<Goods> goodsList = new ArrayList<>();


        for (int i:array
             ) {
            LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Goods::getId,i).eq(Goods::getDeleted,0);
            Goods goods = goodsMapper.selectOne(lqw);
            goodsList.add(goods);
        }
        return goodsList;
    }
    //分页查询商品
    @Override
    public Page<Goods> pageGoods(int page, int pageSize) {
        //构造分页构造器
        Page<Goods> goodsPage = new Page<>();
        //构造过滤条件
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Goods::getDeleted,0);

        return goodsMapper.selectPage(goodsPage,lqw);
    }
    //修改商品信息
    @Override
    public int update(Map param,Long goodsId) {

        //根据商品id查询商品
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Goods::getId,goodsId).eq(Goods::getDeleted,0);
        Goods goods = goodsMapper.selectOne(lqw);

        //修改商品信息
        goods.setGoodsName((String) param.get("goodsName"));
        goods.setPrice(Double.valueOf((String) param.get("price")));
        goods.setGoodsContent((String) param.get("goodsContent"));
        goods.setGoodsTag((String) param.get("goodsTag"));
        goods.setPicture((String) param.get("picture"));
        goods.setReturnRate(Double.valueOf((String) param.get("returnRate")));

        int update = goodsMapper.update(goods, lqw);
        return update;

    }
    //删除商品
    @Override
    public int delete(Long goodsId) {
        //根据对象更新，将delete改为1代表删除
        Goods goods = new Goods();
        goods.setId(goodsId);
        goods.setDeleted(1);

        int i = goodsMapper.updateById(goods);

        return i;

    }
    //根据订单商品id查询商品成本
    @Override
    public Double findCost(Long goodsId) {
        //构造查询条件
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        //仅查询商品成本字段
        lqw.select(Goods::getCost).eq(Goods::getId,goodsId).eq(Goods::getDeleted,0);
        Goods goods = goodsMapper.selectOne(lqw);
        return goods.getCost();

    }

}
